/*
@title: hangman
@description: A version of hangman! Try to guess the word using as few letter guesses as possible.
@author: PapaAlpaca
@tags: ['retro']
@addedOn: 2023-05-05
Special thanks to jedgrei for the alphabet design :)
*/
/*
=====CONTROLS=====
WASD-Movement
I-Select
*/


const cursor = "c"
const leftBound = 1;
const rightBound = 9;
const topBound = 5;
const bottomBound = 7;
const words = ["SPRIG","PROGRAM","PROCESS","BINARY","MEMORY","STORAGE","DISK","DRIVE","KEYBOARD","ALGORITHM","INTERFACE","COMPILER","CONDITION","STRUCTURE","SYNTAX","CLOUD","CYBER"];
let guessed;
let word;
let incorrect;

setLegend(
  [ cursor, bitmap`
..777777777777..
.7............7.
7..............7
7..............7
7..............7
7..............7
7..............7
7..............7
7..............7
7..............7
7..............7
7..............7
7..............7
7..............7
.7............7.
..777777777777..`],
  [ "A", bitmap`
................
................
................
....00000000....
...0........0...
...0........0...
...0........0...
...0........0...
...0000000000...
...0........0...
...0........0...
...0........0...
...0........0...
................
................
................`],
  [ "B", bitmap`
................
................
................
...000000000....
...0........0...
...0........0...
...0........0...
...0........0...
...000000000....
...0........0...
...0........0...
...0........0...
...000000000....
................
................
................`],
  [ "C", bitmap`
................
................
................
....000000000...
...0............
...0............
...0............
...0............
...0............
...0............
...0............
...0............
....000000000...
................
................
................`],
  [ "D", bitmap`
................
................
................
...000000000....
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...000000000....
................
................
................`],
  [ "E", bitmap`
................
................
................
...0000000000...
...0............
...0............
...0............
...0............
...000000000....
...0............
...0............
...0............
...0000000000...
................
................
................`],
  [ "F", bitmap`
................
................
................
...0000000000...
...0............
...0............
...0............
...0............
...000000000....
...0............
...0............
...0............
...0............
................
................
................`],
  [ "G", bitmap`
................
................
................
....000000000...
...0............
...0............
...0............
...0............
...0.....0000...
...0........0...
...0........0...
...0........0...
....00000000....
................
................
................`],
  [ "H", bitmap`
................
................
................
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0000000000...
...0........0...
...0........0...
...0........0...
...0........0...
................
................
................`],
  [ "I", bitmap`
................
................
................
...0000000000...
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
...0000000000...
................
................
................`],
  [ "J", bitmap`
................
................
................
............0...
............0...
............0...
............0...
............0...
............0...
...0........0...
...0........0...
...0........0...
....00000000....
................
................
................`],
  [ "K", bitmap`
................
................
................
...0........0...
...0........0...
...0........0...
...0........0...
...0.......0....
...00000000.....
...0.......0....
...0........0...
...0........0...
...0........0...
................
................
................`],
  [ "L", bitmap`
................
................
................
...0............
...0............
...0............
...0............
...0............
...0............
...0............
...0............
...0............
...0000000000...
................
................
................`],
  [ "M", bitmap`
................
................
................
...0000000000...
...0....0...0...
...0....0...0...
...0....0...0...
...0....0...0...
...0....0...0...
...0....0...0...
...0....0...0...
...0....0...0...
...0........0...
................
................
................`],
  [ "N", bitmap`
................
................
................
...0........0...
...00.......0...
...0.0......0...
...0..0.....0...
...0...0....0...
...0....0...0...
...0.....0..0...
...0......0.0...
...0.......00...
...0........0...
................
................
................`],
  [ "O", bitmap`
................
................
................
....00000000....
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
....00000000....
................
................
................`],
  [ "P", bitmap`
................
................
................
...000000000....
...0........0...
...0........0...
...0........0...
...0........0...
...000000000....
...0............
...0............
...0............
...0............
................
................
................`],
  [ "Q", bitmap`
................
................
................
....000000000...
...0........0...
...0........0...
...0........0...
...0........0...
....000000000...
............0...
............0...
............0...
............0...
................
................
................`],
  [ "R", bitmap`
................
................
................
...000000000....
...0........0...
...0........0...
...0........0...
...0........0...
...000000000....
...0.....0......
...0......0.....
...0.......0....
...0........0...
................
................
................`],
  [ "S", bitmap`
................
................
................
....000000000...
...0............
...0............
...0............
...0............
....00000000....
............0...
............0...
............0...
...000000000....
................
................
................`],
  [ "T", bitmap`
................
................
................
...0000000000...
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
................
................
................`],
  [ "U", bitmap`
................
................
................
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
....00000000....
................
................
................`],
  [ "V", bitmap`
................
................
................
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
...0........0...
....0......0....
.....0....0.....
......0000......
................
................
................`],
  [ "W", bitmap`
................
................
................
...0........0...
...0....0...0...
...0....0...0...
...0....0...0...
...0....0...0...
...0....0...0...
...0....0...0...
...0....0...0...
...0....0...0...
...0000000000...
................
................
................`],
  [ "X", bitmap`
................
................
................
...0........0...
...0.......0....
....0.....0.....
.....0...0......
......0.0.......
.......0........
......0.0.......
.....0...0......
....0.....0.....
...0.......00...
................
................
................`],
  [ "Y", bitmap`
................
................
................
...0........0...
...0.......0....
....0.....0.....
.....0...0......
......0.0.......
.......0........
......0.........
.....0..........
....0...........
...0............
................
................
................`],
  [ "Z", bitmap`
................
................
................
...0000000000...
...........0....
..........0.....
.........0......
........0.......
.......0........
......0.........
.....0..........
....0...........
...0000000000...
................
................
................`],
  [ "_", bitmap`
................
................
................
................
................
................
................
................
................
................
................
................
...0000000000...
................
................
................`],
  [ "g", bitmap`
4..............4
.4............4.
..4..........4..
...4........4...
....4......4....
.....4....4.....
......4..4......
.......44.......
.......44.......
......4..4......
.....4....4.....
....4......4....
...4........4...
..4..........4..
.4............4.
4..............4`],
  [ "x", bitmap`
3..............3
.3............3.
..3..........3..
...3........3...
....3......3....
.....3....3.....
......3..3......
.......33.......
.......33.......
......3..3......
.....3....3.....
....3......3....
...3........3...
..3..........3..
.3............3.
3..............3`],
  [ "|", bitmap`
................
................
................
................
................
................
................
................
................
................
................
............CCCC
............C...
............C...
............C...
............C...`],
  [ "-", bitmap`
................
................
................
................
................
................
................
................
................
................
................
CCCCCCCCC.......
........C.......
........C.......
........C.......
........C.......`],
  [ "]", bitmap`
........C.......
........C.......
........C.......
........C.......
........C.......
........C.......
........C.......
........C.......
........C.......
........C.......
........C.......
........C.......
........C.......
....CCCCCCCCC...
................
................`],
  [ "h", bitmap`
............C...
...........000..
..........0...0.
..........0...0.
..........0...0.
...........000..
................
................
................
................
................
................
................
................
................
................`],
  [ "i", bitmap`
............C...
...........000..
..........0...0.
..........0...0.
..........0...0.
...........000..
............0...
............0...
............0...
............0...
............0...
................
................
................
................
................`],
  [ "j", bitmap`
............C...
...........000..
..........0...0.
..........0...0.
..........0...0.
...........000..
............0...
...........00...
..........0.0...
..........0.0...
............0...
................
................
................
................
................`],
  [ "k", bitmap`
............C...
...........000..
..........0...0.
..........0...0.
..........0...0.
...........000..
............0...
...........000..
..........0.0.0.
..........0.0.0.
............0...
................
................
................
................
................`],
  [ "l", bitmap`
............C...
...........000..
..........0...0.
..........0...0.
..........0...0.
...........000..
............0...
...........000..
..........0.0.0.
..........0.0.0.
............0...
...........0....
..........0.....
..........0.....
................
................`],
  [ "m", bitmap`
............C...
...........000..
..........0...0.
..........0...0.
..........0...0.
...........000..
............0...
...........000..
..........0.0.0.
..........0.0.0.
............0...
...........0.0..
..........0...0.
..........0...0.
................
................`],
  [ "r", bitmap`
................
................
................
....HHHHHHHH....
....H......H....
....H......H....
.........HHHHH..
....H.....HHH...
...HHH.....H....
..HHHHH.........
....H......H....
....H......H....
....HHHHHHHH....
................
................
................`]
)
addText("Welcome to Hangman!",{x:1},{y:1});
let intro = true;
function start() {
  guessed = [];
  word = words[Math.floor(Math.random()*words.length)];
  incorrect = 0;
  setMap(map`
...........
.........|-
..........]
...........
...........
.ABCDEFGHI.
.JKLMNOPQR.
.STUVWXYZc.
...........`)
  for(let i = 0; i < word.length; i++) {
    addSprite(1+i, 3, "_");
  }
  addSprite(9,2,"h");
}

start();

onInput("w", () => {
    if(getFirst(cursor).y > topBound) getFirst(cursor).y -= 1;
});
onInput("a", () => {
    if(getFirst(cursor).x > leftBound) getFirst(cursor).x -= 1;
});
onInput("s", () => {
    if(getFirst(cursor).y < bottomBound) getFirst(cursor).y += 1;
});
onInput("d", () => {
    if(getFirst(cursor).x < rightBound) getFirst(cursor).x += 1;
});
onInput("i", () => {
  let cx = getFirst(cursor).x;
  let cy = getFirst(cursor).y;
  let char = String.fromCharCode("A".charCodeAt()-1+cx+((cy-5)*9));
  clearText();
  if((incorrect < 5 && !tilesWith("_").length == 0) && char.charCodeAt() <= "Z".charCodeAt()) {
    if(!guessed.includes(char) && word.includes(char)) {
      let idx = word.indexOf(char,0);
      while(idx > -1) {
        let x = 1+idx;
        clearTile(x,3);
        addSprite(x,3,char);
        idx = word.indexOf(char,idx+1);
      }
      guessed.push(char);
      addSprite(cx,cy,"g");
    } else {
      incorrect++;
      addSprite(9,2,String.fromCharCode("h".charCodeAt()+incorrect));
      guessed.push(char);
      addSprite(cx,cy,"x");
    }
  } else if(char.charCodeAt() == ("Z".charCodeAt()+1)) {
    start();
  }
});

afterInput(() => {
  if(intro) {
    clearText();
    intro = false;
  }
  if(tilesWith("_").length == 0) {
    addText("Congrats!!", {x:2,y:2});
    addSprite(9,7,"r");
  } else if(incorrect >= 5) {
    addText("You Lose :(\nWord:"+word,{x:2,y:2});
    addSprite(9,7,"r");
  }
});
